package com.xiangxiangjd.controller;

import com.xiangxiangjd.bean.Topic;
import com.xiangxiangjd.controller.helper.$;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.AnswerUserService;
import com.xiangxiangjd.bean.AnswerUser;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-05-10
 */
@Controller
@RequestMapping("/star/answerUser")
public class AnswerUserController extends BaseController {
    @Autowired
    AnswerUserService answerUserService;


    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main() {
        return "AnswerUser/AnswerUserMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addAnswerUserHtml")
    public String addAnswerUserHtml() {
        return "AnswerUser/AnswerUserAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editAnswerUserHtml")
    public String editAnswerUserHtml(String id, Model model) {
        model.addAttribute("answerUser", answerUserService.selectAnswerUserByIdAnswerUser(id));
        return "AnswerUser/AnswerUserEdit";
    }

    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showAnswerUserHtml")
    public String showAnswerUserHtml(String id, Model model) {
        model.addAttribute("answerUser", answerUserService.selectAnswerUserByIdAnswerUser(id));
        return "AnswerUser/AnswerUserShow";
    }


    /**
     * 分页查询AnswerUser
     *
     * @param page       页码
     * @param limit      查几条
     * @param answerUser 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findAnswerUserByPage")
    @ResponseBody
    public ResultPage findAnswerUserByPage(Integer page, Integer limit, AnswerUser answerUser) {
        return answerUserService.findAnswerUserByPage(page, limit, answerUser);
    }

    /**
     * 添加AnswerUser
     *
     * @param answerUser 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addAnswerUser")
    @ResponseBody
    public ApiResultModel saveAnswerUser(AnswerUser answerUser) {
        return answerUserService.addAnswerUser(answerUser);
    }

    /**
     * 编辑AnswerUser
     *
     * @param answerUser 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editAnswerUser")
    @ResponseBody
    public ApiResultModel editAnswerUser(AnswerUser answerUser) {
        return answerUserService.editAnswerUser(answerUser);
    }

    /**
     * 删除AnswerUser
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delAnswerUser", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delAnswerUser(@RequestParam("datas[]") ArrayList ids) {

        return answerUserService.delAnswerUsers(ids);

    }

    /**
     * 用户提交问卷
     *
     *
     * @param answerUsers     所有的题目
     * @return
     */
    @RequestMapping(value = "submitQuestionnaire")
    @ResponseBody
    public ApiResultModel submitQuestionnaire(List<AnswerUser> answerUsers) {

        //用户回答的答案插入
        boolean batch = answerUserService.saveBatch(answerUsers);


        if (!batch) return $.error("插入错误,请重新提交问题！");


        return $.success("提交成功");

    }



}
